import { Component, OnInit, EventEmitter, Input, Output, ElementRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Service } from '../../../server/server';
import { TabVal } from '../privilege-table/privilege-table.component';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;

interface Reponse {
	result: string,
	result_code: string,
	message: string
}

@Component({
	selector: 'app-privilege-edit',
	templateUrl: './privilege-edit.component.html'
})
export class PrivilegeEditComponent implements OnInit {

	@Input()
	public getTabVal: TabVal;

	@Output()
	updateOutput: EventEmitter<any[]> = new EventEmitter();

	constructor(private http: HttpClient, private server: Service, private cudTableService: CudTableService) { }

	ngOnInit() {
		this.resetdata = this.getTabVal;
	}

	submitEdit(val) {

		let data: Edit = new Edit(val.operationId, val.operationCode, val.operationName, Number(val.enable), val.description);
		let url = this.server.baseUrl+'admin/operations/modify';
		this.cudTableService.postRequest(url, data).then((res: any) => {
			if (res.result_code = '0000') {
				$("#edit").modal("hide");
				//prompt
				this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
				this.server.promptclose;
				this.updateOutput.emit(res.result_code);
				//update
			} else {
				this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
				this.server.promptclose;

			}
		}).catch()
	}
	//重置
	public resetdata: any;
	reset() {

		this.getTabVal = this.resetdata;
	}
}

export class Edit {
	constructor(
		public operationId: string,
		public operationCode: string,
		public operationName: string,
		public enable: any,
		public description?: string
	) { }
}
